package com.sg.business.model.sap;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.sg.business.model.Organization;
import com.sg.business.model.Project;
import com.sg.business.model.ProjectBudget;
import com.sg.business.model.User;
import com.sg.business.model.WorkOrderDetail;
import com.sg.business.model.toolkit.UserToolkit;

public class SAPSyncWorkOrder {
	public void syncWorkOrder(Project project, String workOrder,
			WorkOrderDetail workOrderDetail) throws Exception {
		String projectName = project.getDesc();
		Date planStart = project.getPlanStart();
		ProjectBudget projectBudget = project.getBudget();
		Double budgetValue = projectBudget.getBudgetValue();
		String companyCodeValue = workOrderDetail.getCompanyCodeValue();
		String businessScopeCode = workOrderDetail.getBusinessScopeCode();
		String factoryCode = workOrderDetail.getFactoryCode();
		String groupCode = workOrderDetail.getGroupCode();
		User user = UserToolkit.getUserById(workOrderDetail.get_caccount()
				.getUserId());
		String applicant = user.getUsername() + "(" + user.getUserid() + ")";
		Organization org = user.getOrganization();
		String desc = org.getDesc();
		Date applicantDate = workOrderDetail.get_cdate();
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put(ISAPConstants.JCO_WORKORDER_ORDER, workOrder);
		parameter.put(ISAPConstants.JCO_WORKORDER_ORDER_TYPE, "Z002");
		parameter.put(ISAPConstants.JCO_WORKORDER_ORDER_NAME, projectName);
		parameter.put(ISAPConstants.JCO_WORKORDER_CO_AREA, "4000");
		parameter.put(ISAPConstants.JCO_WORKORDER_COMP_CODE, companyCodeValue);
		parameter.put(ISAPConstants.JCO_WORKORDER_BUS_AREA, businessScopeCode);
		parameter.put(ISAPConstants.JCO_WORKORDER_PLANT, factoryCode);
		parameter.put(ISAPConstants.JCO_WORKORDER_APPLICANT, applicant);
		parameter.put(ISAPConstants.JCO_WORKORDER_PERSON_RESP, applicant);
		parameter.put(ISAPConstants.JCO_WORKORDER_ESTIMATED_COSTS, budgetValue);
		parameter.put(ISAPConstants.JCO_WORKORDER_APPLICATION_DATE,
				applicantDate);
		parameter.put(ISAPConstants.JCO_WORKORDER_DEPARTMENT, desc);
		parameter.put(ISAPConstants.JCO_WORKORDER_DATE_WORK_BEGINS, planStart);
		parameter.put(ISAPConstants.JCO_WORKORDER_PROCESSING_GROUP, groupCode);
		parameter.put(ISAPConstants.JCO_WORKORDER_FUNC_AREA, "0500");

		JCO_BAPI_INTERNALORDER_CREATE func = new JCO_BAPI_INTERNALORDER_CREATE();
		func.createWorkOrder(parameter);
	}
}
